
WindTableModel = function() {
	this.toolbar = WindToolbar.createModel();
	this.buttons = new Array();
	this.rows = new Array();
	this.columns = new Array();
}

WindTableModel.prototype.getToolbar = function() {
	return this.toolbar;
}

WindTableModel.prototype.getTitle = function() {
	return this.title;
}

WindTableModel.prototype.setTitle = function(title) {
	this.title = title;
}

WindTableModel.prototype.getNumberOfToolbarButtons = function(button) {
	return this.buttons.length;
}

WindTableModel.prototype.addButton = function(button) {
	return this.buttons[this.buttons.length] = button;
}

WindTableModel.prototype.getToolbarButton = function(index) {
	return this.buttons[index];
}

WindTableModel.prototype.clearButtons = function() {
	this.buttons.length = 0;
}

WindTableModel.prototype.getNumberOfColumns = function() {
	return this.columns.length;
}

WindTableModel.prototype.getNumberOfVisibleColumns = function() {
	var count = 0;
	
	for(var i=0; i<this.getNumberOfColumns(); i++) {
		var column = this.getColumn(i);
		if(column.getVisible()) {
			count++;
		}
	}
	
	return count;
}

WindTableModel.prototype.addColumn = function(column) {
	return this.columns[this.columns.length] = column;
}

WindTableModel.prototype.clearColumns = function() {
	this.columns.length = 0;
}

WindTableModel.prototype.getColumn = function(index) {
	return this.columns[index];
}

WindTableModel.prototype.getNumberOfRows = function() {
	return this.rows.length;
}

WindTableModel.prototype.addRow = function(row) {
	return this.rows[this.rows.length] = row;
}

WindTableModel.prototype.getRow = function(index) {
	return this.rows[index];
}

WindTableModel.prototype.clearRows = function() {
	this.rows.length = 0;
}

WindTableModel.prototype.getCell = function(c, r) {
	var row = this.getRow(r);
	var cell = "a";
	if(c<row.getNumberOfCells()) {
		cell = row.getCell(c);
	}
	return cell;
}

WindTableModel.prototype.setMultipleSelection = function(multipleSelection) {
	this.multipleSelection = multipleSelection;
}

WindTableModel.prototype.getMultipleSelection = function() {
	return this.multipleSelection;
}

WindTableModel.prototype.getSelectedRows = function() {
	var selectedRows = new Array();
	
	for(var i=0; i<this.getNumberOfRows(); i++) {
		var row = this.getRow(i);
		if(row.getSelected()) {
			selectedRows[selectedRows.length] = row;
		}
	}
	
	return selectedRows;
}

WindTableModel.prototype.getView = function() {
	return this.view;
}

WindTableModel.prototype.setView = function(view) {
	this.view = view;
}

WindTableColumnModel = function() {
	this.visible = true;
}

WindTableColumnModel.prototype.getTitle = function() {
	return this.title;
}

WindTableColumnModel.prototype.setTitle = function(title) {
	this.title = title;
}

WindTableColumnModel.prototype.getVisible = function() {
	return this.visible;
}

WindTableColumnModel.prototype.setVisible = function(visible) {
	this.visible = visible;
}

WindTableRowModel = function() {
	this.selected = false;
	this.cells = new Array();
}

WindTableRowModel.prototype.getCell = function(index) {
	return this.cells[index];
}

WindTableRowModel.prototype.getNumberOfCells = function() {
	return this.cells.length;
}

WindTableRowModel.prototype.addCell = function(cell) {
	return this.cells[this.cells.length] = cell;
}

WindTableRowModel.prototype.setSelected = function(selected) {
	this.selected = selected;
}

WindTableRowModel.prototype.getSelected = function() {
	return this.selected;
}


var WindTable = function() {
}

WindTable.createModel = function() {
	var tableModel = new WindTableModel();

	tableModel.setMultipleSelection(true);
	
	tableModel.setTitle("Test");
	{
		var button = new WindButtonModel();
		button.setTitle("Button 1");
		tableModel.addButton(button);
	}
	{
		var button = new WindButtonModel();
		button.setTitle("Button 2");
		tableModel.addButton(button);
	}
	{
		var button = new WindButtonModel();
		button.setTitle("Button 3");
		tableModel.addButton(button);
	}
	{
		var column = new WindButtonModel();
		column.setTitle("Column 1");
		tableModel.addColumn(column);
	}
	{
		var column = new WindButtonModel();
		column.setTitle("Column 2");
		tableModel.addColumn(column);
	}
	{
		var column = new WindButtonModel();
		column.setTitle("Column 3");
		tableModel.addColumn(column);
	}
	{
		var column = new WindButtonModel();
		column.setTitle("Column 4");
		tableModel.addColumn(column);
	}
	{
		var row = new WindTableRowModel();
		tableModel.addRow(row);
		row.addCell("Test A");
		row.addCell("Test B");
		row.addCell("Test C");
		row.addCell("Test D");
	}

	return tableModel;
}

WindTable.createView = function(tableModel) {
	if(tableModel==null) {
		tableModel = WindTable.createModel();
	}

	var tableView = new WindView();
	tableView.setCssClass("WindTable");
	
	tableModel.setView(tableView);
	
	tableView.render = function() {
		var table = document.createElement("table");
		table.cellPadding = 0;
		table.cellSpacing = 0;
		this.setHtmlElement(table);
		this.applyViewAttributesToHtmlElement(table);
		
		this.renderChildren(table);		
		return table;
	}

	var titleView = WindTable.createTitleView(tableModel);
	tableView.addView(titleView);
	
	var toolbarView = WindTable.createToolbarView(tableModel);
	tableView.addView(toolbarView);
	
	var messagesView = WindTable.createMessagesView(tableModel, tableView);
	tableView.addView(messagesView);
	
	var columnsView = WindTable.createColumnsView(tableModel);
	tableView.addView(columnsView);

	WindTable.createEmptyView(tableModel, tableView);

	WindTable.createRowsView(tableModel, tableView);

	return tableView;
}

WindTable.createTitleView = function(tableModel) {
	var titleView = new WindView();
	titleView.setCssClass("WindTableTitle");
	titleView.setText(tableModel.getTitle());

	titleView.render = function() {
		var tr = document.createElement("tr");
		titleView.setHtmlElement(tr);
	
		var td = document.createElement("td");
		this.applyViewAttributesToHtmlElement(td);

		td.colSpan = tableModel.getNumberOfVisibleColumns();
		if(tableModel.getMultipleSelection()) {
			td.colSpan = td.colSpan + 1;
		}
		
		tr.appendChild(td);
		this.renderChildren(td);
		
		return tr;
	}
	return titleView;
}

WindTable.createToolbarView = function(tableModel) {
	var toolbarView = new WindView();
	toolbarView.setCssClass("WindTableToolbar");
	
	var _toolbarView = WindToolbar.createView(tableModel.getToolbar());
	toolbarView.addView(_toolbarView);
	
	for(i=0; i<tableModel.getNumberOfToolbarButtons(); i++) {
		var buttonModel = tableModel.getToolbarButton(i);
		var buttonView = WindButton.createView(buttonModel);
		toolbarView.addView(buttonView);
	}

	toolbarView.render = function() {
		var tr = document.createElement("tr");
		toolbarView.setHtmlElement(tr);
	
		var td = document.createElement("td");
		this.applyViewAttributesToHtmlElement(td);
		
		td.colSpan = tableModel.getNumberOfVisibleColumns();
		if(tableModel.getMultipleSelection()) {
			td.colSpan = td.colSpan + 1;
		}

		tr.appendChild(td);
		this.renderChildren(td);
		
		return tr;
	}
	
	return toolbarView;
}

WindTable.createMessagesView = function(tableModel, tableView) {
	var messagesView = new WindView;
	messagesView.setCssClass("WindTableMessages");
	
	messagesView.render = function() {
		var tr = document.createElement("tr");
		messagesView.applyViewAttributesToHtmlElement(tr);
		
		var td = document.createElement("td");
		td.className = "WindTableMessages";
		tr.appendChild(td);			
		
		td.colSpan = tableModel.getNumberOfVisibleColumns();
		if(tableModel.getMultipleSelection()) {
			td.colSpan = td.colSpan + 1;
		}
		
		messagesView.setHtmlElement(td);
		messagesView.hide();

		messagesView.renderChildren(td);		
		return tr;
	}
	
	tableView.clearMessages = function(error) {
		messagesView.clearViews();
		messagesView.hide();
	}	
	
	tableView.addError = function(error) {
		var errorView = new WindView();
		errorView.setCssClass("WindTableMessageError");
		errorView.setText("Error: " + error);
		messagesView.addView(errorView);
		messagesView.show();
	}
	
	tableView.addWarning = function(warn) {
		var errorView = new WindView();
		errorView.setCssClass("WindTableMessageWarning");
		errorView.setText("Warn: " + warn);
		messagesView.addView(errorView);
		messagesView.show();
	}

	tableView.addInfo = function(info) {
		var errorView = new WindView();
		errorView.setCssClass("WindTableMessageInfo");
		errorView.setText("Info: " + info);
		messagesView.addView(errorView);
		messagesView.show();
	}
	
	return messagesView;
}

WindTable.createColumnsView = function(tableModel) {
	var columnsView = new WindView();
	columnsView.setCssClass("WindTableColumns");
	
	columnsView.render = function() {
		var tr = document.createElement("tr");
		columnsView.setHtmlElement(tr);
		columnsView.applyViewAttributesToHtmlElement(tr);
		
		if(tableModel.getMultipleSelection()) {
			var td = document.createElement("td");
			td.className = "WindTableColumnSelection";
			tr.appendChild(td);			
			
			var check = document.createElement("input")
			check.type = "checkbox";
			td.appendChild(check);			

		}
		
		columnsView.renderChildren(tr);		
		return tr;
	}
	
	for(i=0; i<tableModel.getNumberOfColumns(); i++) {
		var columnModel = tableModel.getColumn(i);
		
		var columnView = new WindView();
		columnView.setCssClass("WindTableColumn");
		columnView.setText(columnModel.getTitle());
		columnsView.addView(columnView);
		
		if(!columnModel.getVisible()) {
			columnView.hide();
		}
		
		columnView.render = function() {
			var td = document.createElement("td");
			this.setHtmlElement(td);
			this.applyViewAttributesToHtmlElement(td);
			this.renderChildren(td);		
			return td;
		}
	}
	
	return columnsView;
}

WindTable.createEmptyView = function(tableModel, tableView) {
	if(tableModel.getNumberOfRows()==0) {
		var rowView = new WindView();
		rowView.setCssClass("WindTableRow");
		tableView.addView(rowView);

		rowView.render = function() {
			var tr = document.createElement("tr");
			this.setHtmlElement(tr);
			this.applyViewAttributesToHtmlElement(tr);
			this.renderChildren(tr);		
			return tr;
		}

		var cellView = new WindView();
		cellView.setCssClass("WindTableCell");
		cellView.setText("Empty");
		rowView.addView(cellView);

		cellView.render = function() {
			var td = document.createElement("td");
			
			td.colSpan = tableModel.getNumberOfVisibleColumns();
			if(tableModel.getMultipleSelection()) {
				td.colSpan = td.colSpan + 1;
			}

			this.setHtmlElement(td);
			this.applyViewAttributesToHtmlElement(td);
			this.renderChildren(td);		
			return td;
		}
	}
}

WindTable.createRowsView = function(tableModel, tableView) {
	for(r=0; r<tableModel.getNumberOfRows(); r++) {
		var rowModel = tableModel.getRow(r);
		
		var rowView = new WindView();
		rowView.setModel(rowModel);
		rowView.setCssClass("WindTableRow");
		tableView.addView(rowView);
		
		rowView.render = function() {
			var tr = document.createElement("tr");
			this.setHtmlElement(tr);
			this.applyViewAttributesToHtmlElement(tr);
			
			if(tableModel.getMultipleSelection()) {
				var td = document.createElement("td");
				td.className = "WindTableRowSelection";
				tr.appendChild(td);			
				
				var check = document.createElement("input")
				check.type = "checkbox";
				td.appendChild(check);			
				
				var rowModel = this.getModel();
				check.addEventListener('click', function() {rowModel.setSelected(check.checked);}, false);
			}

			this.renderChildren(tr);
			
			return tr;
		}
	
		for(c=0; c<tableModel.getNumberOfColumns(); c++) {
			var columnModel = tableModel.getColumn(c);
			
			var cellView = new WindView();
			cellView.setCssClass("WindTableCell");
			
			var text = tableModel.getCell(c, r);
			if(text==null) {
				text = "&nbsp;";
			}
			
			cellView.setText(text);
			rowView.addView(cellView);
			
			if(!columnModel.getVisible()) {
				cellView.hide();
			}
		
			cellView.render = function() {
				var td = document.createElement("td");
				this.setHtmlElement(td);
				this.applyViewAttributesToHtmlElement(td);
				this.renderChildren(td);		
				return td;
			}
		}		
	}
}
